<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
if (!Session::has('DSL','admin')){
	die('非法登录');
}

class Loginstat extends Controller
{
    public function index()
    {
    	$keyword = trim(input('get.keyword'));
    	if ($keyword == ''){
    		$warehouse_list = db('warehouse')->paginate(10);
    	}else {
    		$warehouse_list = db('warehouse')->where('warehouse','like','%'.$keyword.'%')->paginate(10,false,['query' => request()->param()]);
    	}
    	$page = $warehouse_list->render();
    	$this->assign('warehouse_list',$warehouse_list);
    	$this->assign('page',$page);
    	return $this->fetch('index');
    }
    
    public function warehouse_stat()
    {
    	$warehouse_id = input('get.warehouse_id');
    	$warehouse_list = db('warehouse')->where('id',$warehouse_id)->find();
    	$logins_arr = db('loginlog')->where('warehouse_id',$warehouse_id)->column('logins');
		$logins_data = array();
    	foreach ($logins_arr as $lk => &$lv){
    		$lv = json_decode($lv);
    		foreach ($lv as $lvk => &$lvv){
    			$lvv = date('Y-m-d',$lvv);
    			array_push($logins_data, $lvv);
    		}
    	}
    	$logins_data = array_count_values($logins_data);
    	ksort($logins_data);
    	$this->assign('logins_data',json_encode($logins_data));
    	$this->assign('warehouse_list',$warehouse_list);
    	return $this->fetch('warehouse_stat');
    }
    
    public function warehouse_user_list()
    {
    	$warehouse_id = input('get.warehouse_id');
    	$keyword = input('get.keyword');
    	if ($keyword == ''){
    	    $user_list = db('ouser')->where('warehouse_id',$warehouse_id)->paginate(10);
    	}else {
    	    $user_list = db('ouser')->where('user_name','like','%'.$keyword.'%')->paginate(10,false,['query'=>request()->param()]);
    	}
    	$page = $user_list->render();
    	$this->assign('page',$page);
    	$this->assign('user_list',$user_list);
    	$this->assign('warehouse_id',$warehouse_id);
    	return $this->fetch('warehouse_user_list');
    }
    
    public function warehouse_user_stat()
    {
        $warehouse_id = input('get.warehouse_id');
        $user_id = input('get.user_id');
        $user_info = db('ouser')->where('id',$user_id)->find();
        $logins_arr = db('loginlog')->where('user_id',$user_id)->column('logins');
        $logins_data = array();
        foreach ($logins_arr as $lk => &$lv){
            $lv = json_decode($lv);
            foreach ($lv as $lvk => &$lvv){
                $lvv = date('Y-m-d',$lvv);
                array_push($logins_data, $lvv);
            }
        }
        $logins_data = array_count_values($logins_data);
        ksort($logins_data);
        $this->assign('logins_data',json_encode($logins_data));
        $this->assign('user_info',$user_info);
        return $this->fetch('warehouse_user_stat');
    }
    
    public function export_user_logins()
    {
        $user_name = input('get.user_name');
        $user_id = input('get.user_id');
        $logins_arr = db('loginlog')->where('user_id',$user_id)->value('logins');
        $logins_arr = json_decode($logins_arr);
        $logins_data = array();
        foreach ($logins_arr as $lk => &$lv){
            $tmp_arr = array();
            $lv = date('Y-m-d H:i:s',$lv);
            array_push($tmp_arr, $lv);
            array_push($logins_data, $tmp_arr);
        }
        $export_controller = new Ouser();
        $export_user_logins = $export_controller->write_excel(['登录时间'],$logins_data,$user_name.'登录时间导出表',ROOT_PATH.'Uploads'.DS.'export',true);
    }
    
}